<?php

namespace App\Http\Controllers\Circle;

use App\Http\Controllers\BaseController;
use App\Models\CircleCategory;
use App\Models\Circle;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CircleController extends BaseController
{
    public function index(Request $request)
    {
        $param = $this->validate($request,[
            'circle_category_id' => 'integer',
            'keyword' => 'max:255'
        ]);

        $query = Circle::query();

        if (isset($param['circle_category_id']) && $param['circle_category_id']!==''){
            $query->where('circle_category_id',$param['circle_category_id']);
        }

        if (isset($param['keyword']) && $param['keyword'] !==''){
            $query->where('name','like','%'.$param['keyword'].'%');
        }


         $circles =  $query->where('audit_status',2)
                         ->withCount(['article' => function ($q){
                             $q->where('audit_status',1);
                         }])->withCount('people')
                        ->withCount('article')
                        ->orderByDesc('join_num')
                        ->simplePaginate();

         return $this->success($circles);
    }

    public function show(Request $request)
    {
        $param = $this->validate($request,[
            'circle_id' => 'required|integer',
        ]);

        $user_id = auth('api')->id();
        $data = Circle::query()
                        ->selectRaw('circles.*,(select count(id) from user_circles where circle_id=circles.id and user_id='.$user_id.') as is_join')
                        ->with('user:id,avatar,nickname')
                        ->withCount(['article' => function ($q){
                            $q->where('audit_status',1);
                        }])->withCount('people')
                        ->where('id',$param['circle_id'])
                        ->first();

        return $this->success($data);

    }

    public function category()
    {
        $category =  CircleCategory::query()
                        ->where('status',1)
                        ->orderBy('sort')
                        ->orderByDesc('id')
                        ->get();

        return $this->success($category);
    }

    public function join(Request $request)
    {
        $param = $this->validate($request,[
            'circle_id' => 'required'
        ]);
        $user_id = auth('api')->id();

        $join = DB::table('user_circles')
                  ->where('user_id',$user_id)
                  ->where('circle_id',$param['circle_id'])
                  ->exists();

        if ($join){
            return  $this->error('对不起您已经加入过了');
        }

        $result = DB::table('user_circles')->insert([
            'user_id' => $user_id,
            'circle_id' => $param['circle_id']
        ]);
        DB::table('circles')->where('id',$param['circle_id'])->increment('join_num');

        return $result ? $this->success('加入成功') : $this->error('加入失败');
    }


    public function store(Request $request)
    {
        $param = $this->validate($request,[
            'circle_category_id' => 'required',
            'name' => 'required',
            'area' => 'required',
            'intro' => 'required|max:600',
            'logo' => 'required',
            'bg_image' => 'required'
        ]);

        $param['user_id'] = auth('api')->id();

        $circle = Circle::query()->create($param);

        if ($circle->getKey()){
            return $this->success('创建成功');
        }

        return  $this->error('创建失败');
    }
}
